package com.glodon.paas.exception.handler;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.exception.PaasServerException;
import com.glodon.paas.exception.i18n.Message;

@Provider
public class APIExceptionHandler implements ExceptionMapper<PaasServerException> {
	
	public static final Logger LOGGER = LoggerFactory.getLogger(APIExceptionHandler.class);

	@Autowired
	private Message message;

	public Response toResponse(PaasServerException ex) {
		LOGGER.error(ex.getMessage(),ex);
		
//		String viewMessage = message.getText(ex);
		String viewMessage = ex.getMessage();
		if (StringUtils.isEmpty(viewMessage)) {
			viewMessage = ex.getMessage();
		}
		return Response.status(ex.getHttpStatus()).entity(viewMessage).type("text/plain").build();
	}
}
